package com.industrial.premu.controller;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.nacos.api.utils.StringUtils;
import com.industrial.common.core.utils.poi.ExcelUtil;
import com.industrial.common.core.web.controller.BaseController;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.core.web.page.TableDataInfo;
import com.industrial.common.log.annotation.Log;
import com.industrial.common.log.enums.BusinessType;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.premu.domain.PreMuCountyImportTax;
import com.industrial.premu.service.IPreMuCountyImportTaxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


/**
 * 企业自已填写的税收Controller
 *
 * @author ruoyi
 * @date 2024-05-21
 */
@RestController
@RequestMapping("/county/tax/import")
public class PreMuCountyImportTaxController extends BaseController
{
    @Autowired
    private IPreMuCountyImportTaxService preMuCountyImportTaxService;

    /**
     * 查询企业自已填写的税收列表
     */
    @GetMapping("/list")
    public TableDataInfo list(PreMuCountyImportTax preMuCountyImportTax)
    {
        startPage();
        List<PreMuCountyImportTax> list = preMuCountyImportTaxService.selectPreMuCountyImportTaxList(preMuCountyImportTax);
        return getDataTable(list);
    }

    /**
     * 导出企业自已填写的税收列表
     */
    @Log(title = "企业自已填写的税收", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PreMuCountyImportTax preMuCountyImportTax)
    {
        List<PreMuCountyImportTax> list = preMuCountyImportTaxService.selectPreMuCountyImportTaxList(preMuCountyImportTax);
        ExcelUtil<PreMuCountyImportTax> util = new ExcelUtil<PreMuCountyImportTax>(PreMuCountyImportTax.class);
        util.exportExcel(response, list, "企业自已填写的税收数据");
    }

    /**
     * 获取企业自已填写的税收详细信息
     */
    @GetMapping(value = "/{baseId}")
    public AjaxResult getInfo(@PathVariable("baseId") String baseId)
    {
        return success(preMuCountyImportTaxService.selectPreMuCountyImportTaxByBaseId(baseId));
    }

    /**
     * 新增企业自已填写的税收
     */
    @Log(title = "企业自已填写的税收", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PreMuCountyImportTax preMuCountyImportTax)
    {
        return toAjax(preMuCountyImportTaxService.insertPreMuCountyImportTax(preMuCountyImportTax));
    }

    /**
     * 修改企业自已填写的税收
     */
    @Log(title = "企业自已填写的税收", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PreMuCountyImportTax preMuCountyImportTax)
    {
        return toAjax(preMuCountyImportTaxService.updatePreMuCountyImportTax(preMuCountyImportTax));
    }

    /**
     * 删除企业自已填写的税收
     */
    @Log(title = "企业自已填写的税收", businessType = BusinessType.DELETE)
    @DeleteMapping("/{baseIds}")
    public AjaxResult remove(@PathVariable String[] baseIds)
    {
        return toAjax(preMuCountyImportTaxService.deletePreMuCountyImportTaxByBaseIds(baseIds));
    }

    /**
     * 导入企业的数据
     * @param file
     * @param companyType
     */
    @PostMapping("/importCompanyTaxData")
    public AjaxResult importCompanyTaxData(@RequestPart("file") MultipartFile file, @RequestParam Map<String,String> params){
        String companyType = params.get("companyType");
        String year = params.get("year");
        if(StringUtils.isBlank(companyType) || StringUtils.isBlank(year)){
            return AjaxResult.warn("请选择企业类型或者年份");
        }
        return preMuCountyImportTaxService.importCompanyTaxData(file,companyType,year);
    }

    /**
     * 删除当前区县的数据
     * @return
     */
    @DeleteMapping("/deleteCurrentCountyImportData")
    public AjaxResult deleteCurrentCountyImportData(@RequestBody Map<String,String> params){
        String year = params.get("year");
        String companyType = params.get("companyType");
        if(StringUtils.isBlank(year)){
            return AjaxResult.warn("没有获取到年份");
        }
        if(StringUtils.isBlank(companyType)){
            return AjaxResult.warn("请选择需要删除的企业类型");
        }
        //获取到当前用户所属的区县
        String district = SecurityUtils.getLoginUser().getSysUser().getDistrict();
        if(StringUtils.isBlank(district) || district.length() != 6){
            return AjaxResult.warn("获取到用户的区县代码不合法！");
        }
        return preMuCountyImportTaxService.deleteCurrentCountyImportData(year,district,companyType);
    }

    /**
     * 省级审批生效
     * @return
     */
    @PostMapping("/provinceCheck")
    public AjaxResult provinceCheck(@RequestBody Map<String,String> params){
        String county = params.get("county");
        String years = params.get("years");
        if(StringUtils.isBlank(county) || StringUtils.isBlank(years)){
            return AjaxResult.warn("区县或年份没有找到");
        }

        return preMuCountyImportTaxService.provinceCheck(county,years);
    }
}

